package com.example.yuejian.activity;

import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.util.Log;
import android.view.View;
import android.widget.ImageView;
import android.widget.TextView;

import com.alibaba.fastjson.JSON;
import com.blankj.utilcode.util.ActivityUtils;
import com.blankj.utilcode.util.SPUtils;
import com.blankj.utilcode.util.ToastUtils;
import com.bumptech.glide.Glide;
import com.example.yuejian.R;
import com.example.yuejian.bean.ActivityDetailEntity;
import com.example.yuejian.bean.UserInfo;
import com.example.yuejian.common.Url;
import com.example.yuejian.view.TitleView;
import com.zhouyou.http.EasyHttp;
import com.zhouyou.http.callback.SimpleCallBack;
import com.zhouyou.http.exception.ApiException;

import butterknife.BindView;
import me.himanshusoni.quantityview.QuantityView;


public class ActivityDetailGM extends BaseActivity {


    private static final String TAG = "ActivityDetail";
    @BindView(R.id.title_activity)
    TitleView title_activity;
    @BindView(R.id.purchase_num)
    QuantityView quantityView;
    @BindView(R.id.total)
    TextView textView;
    @BindView(R.id.my_tel)
    TextView my_tel;
    @BindView(R.id.my_name)
    TextView my_name;
    @BindView(R.id.name)
    TextView name;
    @BindView(R.id.address)
    TextView address;
    @BindView(R.id.tel)
    TextView tel;
    @BindView(R.id.price)
    TextView price;
    @BindView(R.id.time)
    TextView time;
    @BindView(R.id.imageView)
    ImageView imageView;
    private Intent intent;
    private String aid;
    private Context context;
    private String price_M;
    private Handler mhandler = new mHandler();

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        setContentView(R.layout.activity_detail_gm);
        super.onCreate(savedInstanceState);

        context = getBaseContext();
        initView();
        initDate();
    }

    private void initDate() {
        intent = getIntent();
        aid = intent.getStringExtra ("aid");

        EasyHttp.get("yuejian6/public/index.php/json/activity/details")
                .params("activity_id", aid)
                .execute(new SimpleCallBack<String>() {
                    @Override
                    public void onSuccess(String data) {

                        Message msg = Message.obtain(); // 实例化消息对象
                        msg.what = 0; // 消息标识
                        msg.obj = data; // 消息内容存放

                        mhandler.sendMessage(msg);

                    }

                    @Override
                    public void onError(ApiException error) {
                        Log.d(TAG, "onFailed: " + error);
                    }
                });

        String uid = SPUtils.getInstance().getInt("uid", 0) + "";
        EasyHttp.get("yuejian6/public/index.php/json/index/userinfo")
                .params("user_id", uid)
                .execute(new SimpleCallBack<String>() {
                    @Override
                    public void onSuccess(String data) {

                        Message msg = Message.obtain(); // 实例化消息对象
                        msg.what = 1; // 消息标识
                        msg.obj = data; // 消息内容存放

                        mhandler.sendMessage(msg);

                    }

                    @Override
                    public void onError(ApiException error) {
                        Log.d(TAG, "onFailed: " + error);
                    }
                });


    }

    private void initView() {
        title_activity.getLeftBackTextTv().setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                ActivityUtils.finishActivity(ActivityDetailGM.class);
            }
        });
        quantityView.setOnQuantityChangeListener(new QuantityView.OnQuantityChangeListener() {
            @Override
            public void onQuantityChanged(int oldQuantity, int newQuantity, boolean programmatically) {
                textView.setText("合计 :" + Double.parseDouble(price_M) * newQuantity + "元");
            }

            @Override
            public void onLimitReached() {

            }
        });
        quantityView.setQuantityDialog(false);


    }

    // 步骤1：自定义Handler子类（继承Handler类） & 复写handleMessage（）方法
    class mHandler extends Handler {
        @Override
        public void handleMessage(Message msg) {
            super.handleMessage(msg);

            switch (msg.what) {
                case 0:

                    Log.d(TAG, "handleMessage: " + msg.obj.toString());
                    ActivityDetailEntity item = null;
                    try {
                        item = JSON.parseObject(msg.obj.toString(), ActivityDetailEntity.class);
                    } catch (Exception e) {
                        ToastUtils.showShort("获取数据失败");
                        return;
                    }


                    name.setText("名字： " + item.getName());
                    address.setText("地址： " + item.getAddress());
                    tel.setText("电话： " + item.getTel());
                    price_M = item.getPrice();
                    textView.setText("合计 :" + Double.parseDouble(item.getPrice() + "") + "元");
                    price.setText("价格：" + item.getPrice());
                    time.setText("时间：" + item.getTime());
                    Glide.with(context)
                            .load(Url.imgUrl + item.getPic())
                            .into(imageView);
                    break;
                case 1:
                    UserInfo userInfo = null;

                    try {

                        userInfo=JSON.parseObject(msg.obj.toString(), UserInfo.class);
                    }catch (Exception e){
                        ToastUtils.showShort("获取数据失败");
                        return;
                    }
                    my_name.setText("当前用户： " + userInfo.getReal_name());
                    my_tel.setText("联系电话： " + userInfo.getPhone());

                    break;
            }

        }
    }


}
